package com.cloudlead.fleetAction.service.impl;

import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.commonData.service.BaseFleetChargeService;
import com.cloudlead.commonData.vo.BaseCompany;
import com.cloudlead.commonData.vo.BaseFleetCharge;
import com.cloudlead.fleetAction.service.FleetChargeSer;
import com.cloudlead.fleetService.vo.BaseFleetChargeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Will on 2015/12/22.
 */
@Service
public class FleetChargeSerImpl implements FleetChargeSer {
    @Autowired
    BaseFleetChargeService baseFleetChargeService;
    @Override
    public String addFleetChargeSerive(BaseFleetChargeVo baseFleetChargeVo) {
        BaseFleetCharge baseFleetCharge = new BaseFleetCharge();
        BaseCompany baseCompany = new BaseCompany();
        if(null != baseFleetChargeVo.getBaseCompanyId() && !"".equals(baseFleetChargeVo.getBaseCompanyId())){
           baseCompany.setId(baseFleetChargeVo.getBaseCompanyId());
        }
        BeanUtils.copyProperties(baseFleetChargeVo, baseFleetCharge);
        baseFleetCharge.setBaseCompany(baseCompany);
        baseFleetChargeService.create(baseFleetCharge);
        return "ok";
    }

    @Override
    public String delFleetChargeSerive(String fleetChargeId) {
        BaseFleetCharge baseFleetCharge = baseFleetChargeService.view(fleetChargeId);
        baseFleetCharge.setFlag("1");
        baseFleetChargeService.update(baseFleetCharge);
        return "ok";
    }

    @Override

    public String editFleetChargeSerive(BaseFleetChargeVo baseFleetChargeVo) {
        BaseFleetCharge baseFleetCharge = new BaseFleetCharge();
        BaseCompany baseCompany = new BaseCompany();
        if(null != baseFleetChargeVo.getBaseCompanyId() && !"".equals(baseFleetChargeVo.getBaseCompanyId())){
            baseCompany.setId(baseFleetChargeVo.getBaseCompanyId());
        }
        BeanUtils.copyProperties(baseFleetChargeVo, baseFleetCharge);
        baseFleetCharge.setBaseCompany(baseCompany);
        baseFleetChargeService.update(baseFleetCharge);
        return "ok";
    }

    @Override
    public Page findAll(BaseFleetChargeVo baseFleetChargeVo, int rows, int page) {
        PageRequest pageRequest = new PageRequest();
        List<Term> list = new ArrayList<Term>();
        list.add(new Term("flag","0"));
        if(null != baseFleetChargeVo.getBaseCompanyId() && !"".equals(baseFleetChargeVo.getBaseCompanyId())){
            list.add(new Term("baseCompany.id",baseFleetChargeVo.getBaseCompanyId()));
        }
        if(null != baseFleetChargeVo.getCode() && !"".equals(baseFleetChargeVo.getCode())){
            list.add(new Term("code",Operator.LK,baseFleetChargeVo.getCode()));
        }
        if(null != baseFleetChargeVo.getCostName() && !"".equals(baseFleetChargeVo.getCostName())){
            list.add(new Term("costName",Operator.LK,baseFleetChargeVo.getCostName()));
        }
        Filter filter  = new Filter(new Criteria(list));
        pageRequest.setFilter(filter);
        pageRequest.setSize(rows);
        pageRequest.setPage(page);
        return baseFleetChargeService.findAll(pageRequest);
    }
}
